package com.hc.javabasestudyproject.juc.fastsafefail;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: huangcheng
 * @CreateTime: 2024-04-10  16:53
 * @Description:
 * @Version: 1.0
 */
public class FastFailTest {
    public static void main(String[] args){
        List<String> myList=new ArrayList<>(3);
        myList.add("Java");
        myList.add("Python");
        myList.add("C++");
        Iterator<String> iterator =myList.iterator();
        while(iterator.hasNext()){
            String language = iterator.next();
            System.out.println(language);
            // 模拟在迭代过程中修改集合结构
            if(language.equals("Java")){
                // 直接修改集合会抛出异常导致fail-fast
                myList.remove("Python");
                // 迭代器修改，不会抛出异常
                // iterator.remove();
            }
        }
    }
}
